package pr1.cromosoma;

import java.util.Arrays;
import java.util.List;

public class CromosomaF5 extends Cromosoma<List<Double>> {

    public final static double XMAX = 10;
    public final static double XMIN = -10;

	private final int longX;

    public CromosomaF5(int longX) {
        super(2 * longX);
        this.longX = longX;
        setAptitud(evalua());
    }

    @Override
    public CromosomaF5 clone() {
        CromosomaF5 clon = new CromosomaF5(longX);
        clon.copia(this);
        return clon;
    }

    public void copia(CromosomaF5 otro) {
        assert(this.longX == otro.longX);
        super.copia(otro);
    }

    @Override
	public final double evalua() {
        return sumatorio(getX1()) * sumatorio(getX2());
	}

    private double sumatorio(double x) {
        double sum = 0;
        for(int i = 1; i < 6; i++) {
            sum += i * Math.cos((i + 1) * x + i);
        }
        return sum;
    }

	public double getX1() {
		return XMIN + (XMAX - XMIN) * bin2dec(0, longX)
				/ (Math.pow(2, longX) - 1);
	}

    public double getX2() {
		return XMIN + (XMAX - XMIN) * bin2dec(longX, longX)
				/ (Math.pow(2, longX) - 1);
	}

    @Override
    public List<Double> getFenotipo() {
        return Arrays.asList(new Double[] {getX1(), getX2()});
    }

	public static class Factory implements CromosomaFactory {

		double precision;
		int longCromosoma;
        int n;

        public int getN() {
            return n;
        }

        public void setN(int n) {
            this.n = n;
        }

        public double getPrecision() {
			return precision;
		}

		public void setPrecision(double precision) {
			this.precision = precision;
			longCromosoma = (int) Math.ceil(Math.log(1 + (XMAX - XMIN)
					/ precision)
					/ Math.log(2));
		}

		public Cromosoma generaCromosoma() {
			return new CromosomaF5(longCromosoma);
		}
		
        @Override
		public String toString() {
			return "Cromosoma 5";
		}
	}

}
